﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBConnectionLib;


namespace TestDBSourceToolsLibNUnit
{

    [TestFixture]
    public class TestSourceDatabaseLoader
    {
        [Test]
        public void TestLoadFromDisk()
        {
            //Constants.AdventureWorks_ProjectFile
            string strProjectFile = Constants.AdventureWorks_ProjectFile;
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strProjectFile);

            oDBProject.LoadFromDisk(false);
            Assert.AreEqual("AdventureWorks", oDBProject.ProjectName);
            Assert.Greater(oDBProject.SourceDatabases.Count, 0);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].DatabaseName);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].NickName);
            Assert.AreEqual(2, oDBProject.SourceDatabases[0].TargetDatabases.Count, "Source should contain a targetdb");

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(oForm, 
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);

            oSourceLoader.Execute(ref oSource, null);
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Table)
                , 12, "Should load 12 table");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.View)
                , 3, "Should load 3 views");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Function)
                , 3, "Should load 3 functions");

            Assert.IsTrue(oSource.ScriptOptions.ScriptData, "Script Data in ScriptOptions should be true");
            Assert.IsTrue(oSource.ScriptOptions.ScriptDropAndCreate, "Script Data in ScriptOptions should be true");
            Assert.IsTrue(oSource.ScriptOptions.ScriptStoredProcs, "Script Data in ScriptOptions should be true");
            Assert.IsTrue(oSource.ScriptOptions.ScriptTables, "Script Data in ScriptOptions should be true");
            Assert.IsTrue(oSource.ScriptOptions.ScriptUsers, "Script Data in ScriptOptions should be true");
            Assert.IsTrue(oSource.ScriptOptions.ScriptViews, "Script Data in ScriptOptions should be true");

            Assert.IsTrue(oSource.ScriptOptions.MustScriptTable("BuildVersion"), "Must script table TestTable_1 should be true");
            Assert.IsTrue(oSource.ScriptOptions.MustScriptView("SalesLT.vGetAllCategories"), "Must script view TestView_1 should be true");
            Assert.IsTrue(oSource.ScriptOptions.MustScriptData("BuildVersion"), "Must script data TestView_1 should be true");
            Assert.IsTrue(oSource.ScriptOptions.MustScriptProc("uspLogError"), "Must script proc TestProc should be true");

        }

        [Test]
        public void TestLoadFromDatabase()
        {

            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(Constants.TestFiles_Output_ScriptDatabaseDir);

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                Constants.TestFiles_Output_ScriptDatabaseDir, "AdventureWorksLT");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDBSourceToolsTestDB());

            oDBProject.CreateBaseDirectories();

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);

            oSourceLoader.Execute(ref oSource, null);
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Table)
                , 12, "Should load 12 table");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.View)
                , 3, "Should load 3 view");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Function)
                , 3, "Should load 3 function");

        }

        [Test]
        public void TestReloadQueriesFromDisk()
        {

            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(Constants.TestFiles_Output_ScriptDatabaseDir);

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                Constants.TestFiles_Output_ScriptDatabaseDir, "AdventureWorksLT");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDBSourceToolsTestDB());

            oDBProject.CreateBaseDirectories();

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);

            oSourceLoader.Execute(ref oSource, null);
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Table)
                , 12, "Should load 12 table");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.View)
                , 3, "Should load 3 view");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Function)
                , 3, "Should load 3 function");

            FileUtils oFileUtils = new FileUtils();
            {
                string strNewQueryName = Constants.TestFiles_Output_ScriptDatabase_AdventureWorksLT_Queries + "Query1.sql";
                oFileUtils.EnsureFileDirectoryExists(strNewQueryName);
                using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(strNewQueryName))
                {
                    oWriter.WriteLine("Query1");
                }

                DBSourceToolsLib.SourceDatabaseLoader oSourceReloaderLoader =
                    new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadQueriesFromDisk);
                oSourceReloaderLoader.Execute(ref oSource, null);
                Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Querie), 1, "Should load query");
            }

            {
                string strNewQueryName = Constants.TestFiles_Output_ScriptDatabase_AdventureWorksLT_Queries + "Query2.sql";
                using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(strNewQueryName))
                {
                    oWriter.WriteLine("Query1");
                }

                DBSourceToolsLib.SourceDatabaseLoader oSourceReloaderLoader =
                    new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadQueriesFromDisk);
                oSourceReloaderLoader.Execute(ref oSource, null);
                Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Querie), 2, "Should load query");
            }

        }


    }
}
